﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Velocity4Net.Ctx;

namespace Velocity4Net.App.Events
{
    /**
      * Defines the execution strategy for includeEvent
      */
    class IncludeEventExecutor : EventHandlerMethodExecutor
    {
        private IContext context;
        private String includeResourcePath;
        private String currentResourcePath;
        private String directiveName;

        private bool executed = false;

        public IncludeEventExecutor(
                IContext context,
                String includeResourcePath,
                String currentResourcePath,
                String directiveName)
        {
            this.context = context;
            this.includeResourcePath = includeResourcePath;
            this.currentResourcePath = currentResourcePath;
            this.directiveName = directiveName;
        }

        /**
         * Call the method includeEvent()
         *  
         * @param handler call the appropriate method on this handler
         */
        public void execute(EventHandler handler)
        {
            IncludeEventHandler eh = (IncludeEventHandler) handler;
            
            //if (eh is ContextAware)
            //    ((ContextAware) eh).setContext(context);

            executed = true;
            includeResourcePath = ((IncludeEventHandler) handler)
                .includeEvent(includeResourcePath, currentResourcePath, directiveName); 
        }

        public Object getReturnValue()
        {
            return includeResourcePath;
        }

        public bool isDone()
        {
            return executed && (includeResourcePath == null);
        }


    }
}
